/**
 * Created by Administrator on 2017/8/25 0025.
 */

operateFormatter = function (value, row, index) {
    return [
        '<a class="examine" href="javascript:void(0)" >查看详细</a>' +
        '&nbsp;&nbsp;' +
        '<a class="update" href="javascript:void(0)" >编辑商品</a>' +
        '&nbsp;&nbsp;' +
        '<a class="status" href="javascript:void(0)" style="color: red">删除</a>'
    ].join('');
};

window.operateEvents = {
    'click .examine': function (e, value, row) {
        showModel("examine-div", function () {
            $("#id").val(row.id);
            $("#nameGoods").val(row.scoreGoodsName);
            $("#hotLine").val(row.hotLine);
            $("#needScore").val(row.needScore);
            $("#price").val(row.marketPrice);
            $("#stock").val(row.stock);

            var divs = $("#examine-div");
            var imgDiv = divs.find("#show_img");
            var imgs = row.goodsPic;
            var pic = imgs.split(",");
            for (var i = 0; i < pic.length; i++) {
                var imgPath = pic[i];
                var div = File_Download_Url + imgPath;
                imgDiv.append('<img src="' + div + '" width="150" height="150" style="margin-left: 2%" onclick="enlargeImage(this)">');
            }
            $("#photo").attr("src", "" + File_Download_Url + "/" + row.listPic);
            UE.getEditor("UEditor").setContent(row.remark);
        })
    },
    'click .update': function (e, value, row) {
        showModel("updateInfo-div", function () {
            $("#id").val(row.id);
            $("#nameGoodsUpdate").val(row.scoreGoodsName);
            $("#hotLineUpdate").val(row.hotLine);
            $("#needScoreUpdate").val(row.needScore);
            $("#priceUpdate").val(row.marketPrice);
            $("#stockUpdate").val(row.stock);
            $("#image").attr("src", "" + File_Download_Url + "/" + row.goodsPic);
            $("#image").attr("img-path", row.goodsPic);
            $("#listPicture").attr("src", "" + File_Download_Url + "/" + row.listPic);
            $("#listPicture").attr("img-path", row.listPic);

            var divs = $("#updateInfo-div");
            var imgDiv = divs.find("#update_img");
            var imgs = row.goodsPic;
            var pic = imgs.split(",");
            for (var i = 0; i < pic.length; i++) {
                if (pic[i] != "") {
                    var imgPath = pic[i];
                    var div = createImgDiv(File_Download_Url + imgPath);
                    var img = div.find("img");
                    img.attr("is_up_company", 1);
                    img.attr("img-path", imgPath);
                    imgDiv.append(div);
                }
            }
            imgDiv.append(createAddImgDiv());

            UE.getEditor("editors").setContent(row.remark);
        })
    },
    'click .status': function (e, value, row) {
        comment.showOperationTips("确定删除吗？", function () {
            $.post($().AbsUrlParse('/web/score/updateState'), {id: row.id}, function (data) {
                if (data.state == true) {
                    if (data.res.code == 40000) {
                        hint("删除成功");
                        var table = $("#table");
                        table.bootstrapTable("refresh");
                    } else {
                        hint(data.res.msg);
                    }
                } else {
                    hint(data.msg);
                }
            });
        });
    }
};

/**
 * 添加
 */
bindClick("#addInfo", function () {
    showAddCategory();
});

/**
 * 弹出添加框
 */
var showAddCategory = function () {
    showModel("addInfo-div", function () {
        //清空
        $("input[name=goodsScore]").val('');
        $("input[name=hotLine]").val('');
        $("input[name=needScore]").val('');
        $("input[name=marketPrice]").val('');
        $("input[name=stock]").val('');
        editor.setContent('');
    });
};

/**
 * 确认添加按钮事件
 */
bindClick("button[data-event=submit-addInfo]", function () {
    var div = $("#addInfo-div");
    var price = $("#marketPrice").val();
    var js = /^[0-9]*[1-9][0-9]*$/;
    // var phone = /^((0\d{2,3}-\d{7,8})|(1[3584]\d{9}))$/;
    //商品图片
    var imgs = div.find("img[data-event=add_image]");
    var path = "";
    $.each(imgs, function () {
        var mp = $(this).attr("img-path");
        if (mp != null && mp != '') {
            path += mp + ",";
        }
    });
    if (path.indexOf(",") != -1) {
        path = path.substring(0, path.length - 1);
    }

    div.find("input[name=goodsPic]").val(path);
    //列表图片
    var image = $("#pictures").attr("img-path");
    $("#listPic").val(image);

    var content = UE.getEditor('editor').getContent();
    $("#remark").text(content);

    var goodsScore = $("#goodsScore").val();
    var hotLines = $("#hotLines").val();
    var needScores = $("#needScores").val();
    var stocks = $("#stocks").val();

    var form = $("#addInfoFrom");
    var param = form.formToArray();

    if (goodsScore != null && goodsScore != "") {
        if (needScores != null && needScores != "") {
            if (stocks != null && stocks != "") {
                if (js.test(needScores)) {
                    $.post($().AbsUrlParse('/web/score/addGoodsScore'), param, function (res) {
                        if (res.state && res.res.code == 40000) {
                            hint('添加成功！');
                            //关闭model方法
                            closeModel('addInfo-div');
                            var table = $("#table");
                            table.bootstrapTable("refresh");
                        } else {
                            if (res.state == false) {
                                hint(res.msg);
                            } else {
                                hint(res.res.msg);
                            }
                        }
                    });
                } else {
                    hint("积分和库存请输入非小数和非负数的整数，并且库存要大于0");
                }
            } else {
                hint("请添加兑换商品库存");
            }
        } else {
            hint("请添加兑换积分");
        }
    } else {
        hint("请添加积分商品名称");
    }
});

/**
 * 修改按钮事件
 */
bindClick("button[data-event=submit-updateInfo]", function () {
    var div = $("#updateInfo-div");
    var imgs = div.find("img[data-event=add_image]");
    var path = "";
    $.each(imgs, function () {
        var mp = $(this).attr("img-path");
        if (mp != null && mp != '') {
            path += mp + ",";
        }
    });
    if (path.indexOf(",") != -1) {
        path = path.substring(0, path.length - 1);
    }

    div.find("input[name=goodsPic]").val(path);

    var price = $("#priceUpdate").val();
    var needScoreUpdate = $("#needScoreUpdate").val();
    var stockUpdate = $("#stockUpdate").val();
    var js = /^[0-9]*[1-9][0-9]*$/;

    var ima = $("#image").attr("img-path");
    $("#picture").val(ima);

    var image = $("#listPicture").attr("img-path");
    $("#list").val(image);

    var con = UE.getEditor('editors').getContent();
    $("#remarkes").text(con);

    var form = $("#updateInfoFrom");
    var param = form.formToArray();

    var hotLine = $("#hotLineUpdate").val();

    if (js.test(needScoreUpdate) && js.test(stockUpdate)) {
        $.post($().AbsUrlParse('/web/score/updateGoodsScore'), param, function (res) {
            if (res.state && res.res.code == 40000) {
                hint('修改成功！');
                //关闭model方法
                closeModel('updateInfo-div');
                var table = $("#table");
                table.bootstrapTable("refresh");
            } else {
                if (res.state == false) {
                    hint(res.msg);
                } else {
                    hint(res.res.msg);
                }
            }
        });
    } else {
        hint("积分和库存请输入非小数和非负数的整数，并且库存要大于0");
    }
});


function clicke() {
    var goods = $("#goods").val();
    var url = $().AbsUrlParse("/web/score/queryGoodsScore?name=" + goods);
    var table = $("#table");
    table.bootstrapTable("refresh", {url: url});
}

goodsPic = function (value, row, index) {
    var img = row.listPic;
    var imgUrl = "";
    if (img != null && img != "") {
        imgUrl = File_Download_Url + img;
        return '<image src=' + imgUrl + ' style="width:80px;height:80px"  onclick="enlargeImage(this)"/>';
    }
    if (img == null || img == "") {
        return '<image src=' + $().AbsUrlParse("/static/images/no.png") + ' style="width:80px;height:80px"  onclick="enlargeImage(this)"/>'
    }
};

var add_image = $("img[data-id=add_image]");

var imageLoadAfter = function (t) {
    var isUpdate = $(t).attr("img-isUpdate");
    if (isUpdate == "false") {
        var nPath = $().AbsUrlParse("/static/images/commons/img_add.png");
        var div = createImgDiv(nPath);
        var img = div.find("img[data-id=add_image]");

        img.bind("loadImageAfter", function () {
            imageLoadAfter(this);
        });
        if ($(t).attr("is_up_company") != null) {
            img.attr("is_up_company", "1");
            $("#update_img").append(div);
        } else {
            $("#add_img").append(div);
        }
    }
};

add_image.bind("loadImageAfter", function () {
    imageLoadAfter(this);
});


var createImgDiv = function (nPath) {
    var img = $('<img class="addImage" data-id="add_image" data-event="add_image" src="' + nPath + '" width="150" height="150">');
    var div = $('' +
        '<div class="panel-body col-md-3 delImage">' +
        '<div class="row">' +
        '<div data-id="image_div" class="col-md-12">' +
        '&nbsp;&nbsp;&nbsp;&nbsp;' +
        '<div class="col-md-12">' +
        '<i data-id="delete_image" onclick="deleteImage(this)" class="fa fa-times"></i>' +
        '</div>' +
        '<div data-id="div_image" class="col-md-10">' +
        '</div>' +
        '</div>' +
        '</div>' +
        '</div>');
    var imgdiv = div.find("div[data-id=div_image]");
    imgdiv.append(img);

    return div;
};


// var createImgDivtwo = function (nPath) {
//     var img = $('<img  data-id="add_image" data-event="add_image" src="' + nPath + '" width="100%" >');
//     var div = $('' +
//         '<div class="panel-body col-md-3 delImage">' +
//         '<div class="row">' +
//         '<div data-id="image_div" class="col-md-6">' +
//         '<div class="col-md-6">' + '</div>' + '<div data-id="div_image" class="col-md-11">' + '</div>' + '</div>' + '</div>' + '</div>');
//     var imgdiv = div.find("div[data-id=div_image]");
//     imgdiv.append(img);
//     return div;
// };

/**
 * 删除图片
 * @param obj   当前div信息
 */
var deleteImage = function (obj) {
    $(obj).parents("div[data-id=image_div]").remove();
    $(obj).parents("div[data-id=image_div]").hide();
};

var createAddImgDiv = function () {
    var nPath = $().AbsUrlParse("/static/images/commons/img_add.png");
    var img = $('<img class="addImage" is_up_company="1"  data-id="add_image" data-event="add_image" src="' + nPath + '" width="150" height="150">');
    var div = $('' +
        '<div class="panel-body col-md-3  isFirstAdd delImage">' +
        ' <div class="row">' +
        '    <div data-id="image_div" class="col-md-6">' +
        '       <div class="col-md-6">' +
        '            <i data-id="delete_image" onclick="deleteImage(this)" class="fa fa-times"></i>' +
        '       </div>' +
        '<div data-id="div_image" class="col-md-11">' +
        '</div>' +
        '</div>' +
        '</div>' +
        '</div>');
    var imgdiv = div.find("div[data-id=div_image]");
    imgdiv.append(img);

    img.bind("loadImageAfter", function () {
        imageLoadAfter(this);
    });

    return div;
}

function enlargeImage(value) {
    showModel("detailImage-div", function () {
        var imgs = '<image src=' + value.src + ' style="height:500px" />';
        $("#image").html(imgs);
    })
}